package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementMapper statementService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {

        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        StatementItem old = this.get(statementItem.getId());

        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void saveItem(List<StatementItem> list) {
        StatementItem statementItem = list.remove(list.size() - 1);
        if (list==null || list.size()==0 ){
            throw new BusinessException("没有消费单项无法结算");
        }

        // 折扣金额
        BigDecimal discountAmount = statementItem.getDiscountAmount();
        Long statementId = statementItem.getStatementId();
        this.delete(statementId);
        //总金额
        BigDecimal totalPrice= new BigDecimal(0);
        //总数量
        BigDecimal totalQuantity = new BigDecimal(0);
        for (StatementItem item : list) {
            this.save(item);

            totalPrice = totalPrice.add(item.getItemPrice().multiply(item.getItemQuantity()));
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        System.out.println("====================================================================");
        System.out.println(totalPrice);
        System.out.println(totalQuantity);


        statementService.updateConsume(statementId,totalPrice,totalQuantity,discountAmount);
    }

    @Override
    public void delete(Long id) {
        statementItemMapper.deleteByPrimaryKey(id);
    }
}